# 0 leds
# 1 switches
# 2 seven segs
# 3 keypad
# 4 stepper motor
# just send the switches straight to the status
# and set the speed constant (default) for now

### "Kernel" code ###

addi $t0, $0, 0x0C     # isr address
mtc0 $t0, $15          # set ebase to isr address

j start                # go to start

isr: mfc0 $k0, $13     # get the cause register

slti $k1, $k0, 1
addi $k1, $k1, 2
sw $k1, ($s0)

#andi $k1, $k0, 0x2000  # mask keypad interrupt (bit 13)
#beq $k1, $0, isr0      # if not keypad, skip handler
jal keypad             # service keypad
#eret                   # completed isr

#isr0: andi $k1, $k0, 0x0800 # mask switch interrupt (bit 11)
#beq $k1, $0, isr1      # if not switch, skip handler
jal switches           # service switches
isr1: eret             # otherwise return, nothing else handled

# subroutines used as specific interrupt routines

keypad: lw $k1, 0($s3) # load hexkeypad buffer
andi $k1, $k1, 0xFFFF  # get only latest four buttons
add $k1, $k1, $s5      # write buttons to the seven segs
sw $k1, 4($s2)         # write to seven segs
sub $k1, $k1, $s5      # get back just the buttons
sll $k1, $k1, 8        # shift the four buttons left 8 bits
sw $k1, 4($s4)         # set as stepper speed word
jr $ra

switches: lw $k1, ($s1)# load switches
sw $k1, ($s4)          # set stepper motor status
jr $ra

### "USER" code ###

start: addi $t0, $0, 0x2801 # create status word
mtc0 $t0, $12          # set the status word

lui $s0, 0xFFFF        # set base address for leds
addi $s1, $s0, 0x0040  # set base address of the switches controller
addi $s2, $s0, 0x0080  # set base address for seven seg controller
addi $s3, $s0, 0x00C0  # set base address of the hex keypad controller
addi $s4, $s0, 0x0100  # set base address of the stepper motor controller

addi $t0, $0, 0x000F   # turn all seven segs on
lui $s5, 0x00F0        # set mask for write enables and no dots
sw $t0, 0($s2)         # turn all seven segs on
sw $s5, 4($s2)         # write zeros without dots

addi $t0, $0, 4        # set the interrupt
sw $t0, 0xC($s3)       # interval to 4

loop: j loop           # repeat, while we wait for interrupts
